#include "stdafx.h"
#include "wf_drawarctangential.h"

#include "pf_commands.h"
#include "pf_snapper.h"
#include "pf_dialogfactory.h"



WF_DrawArcTangential::WF_DrawArcTangential(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw arcs tangential",
                           container, graphicView) {
    reset();
}



WF_DrawArcTangential::~WF_DrawArcTangential() {}


QAction* WF_DrawArcTangential::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*     QAction* action = new QAction(tr("Arc: Tangential"),
                                  tr("&Tangential"),
                                  QKeySequence(), NULL); */
    QAction* action = new QAction(tr("Arc: Tangential"), NULL);
    //action->zetStatusTip(tr("Draw arcs tangential to base entity"));
    return action;
}


void WF_DrawArcTangential::reset() {
    baseEntity = NULL;
    isStartPoint = false;
    point = PF_Vector(false);
}



void WF_DrawArcTangential::init(int status) {
    PF_PreviewActionInterface::init(status);

    //reset();
}



void WF_DrawArcTangential::trigger() {
    PF_PreviewActionInterface::trigger();

    if (point.valid==false || baseEntity==NULL) {
        PF_DEBUG->print("WF_DrawArcTangential::trigger: "
                        "conditions not met");
        return;
    }

    preparePreview();
    PF_Arc* arc = new PF_Arc(container, data);
    arc->setLayerToActive();
    arc->setPenToActive();
    container->addEntity(arc);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(arc);
        document->endUndoCycle();
    }

	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(arc->getCenter());

    setStatus(SetBaseEntity);
    reset();
}



void WF_DrawArcTangential::preparePreview() {
    if (baseEntity!=NULL && point.valid) {
        PF_Vector startPoint;
        double direction;
        if (isStartPoint) {
            startPoint = baseEntity->getStartpoint();
            direction = PF_Math::correctAngle(baseEntity->getDirection1()+M_PI);
        } else {
            startPoint = baseEntity->getEndpoint();
            direction = PF_Math::correctAngle(baseEntity->getDirection2()+M_PI);
        }

        PF_Arc arc(NULL, PF_ArcAttribute());
        bool suc = arc.createFrom2PDirectionRadius(startPoint, point, direction, data.radius);
        if (suc) {
            data = arc.getData();
        }
    }
}


void WF_DrawArcTangential::mouseMoveEvent(PF_MouseEvent* e) {
    switch (getStatus()) {
    case SetBaseEntity:
        break;

    case SetEndAngle: {
            point = snapPoint(e);
            preparePreview();
            if (data.isValid()) {
                PF_Arc* arc = new PF_Arc(preview, data);
                deletePreview();
                preview->addEntity(arc);
                drawPreview();
            }
        }
        break;

    default:
        break;
    }
}



void WF_DrawArcTangential::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {

            // set base entity:
        case SetBaseEntity: {
                PF_Vector coord = graphicView->toGraph(e->x(), e->y());
                PF_Entity* entity = catchEntity(coord, LX_Define::ResolveAll);
                if (entity!=NULL) {
                    if (entity->isAtomic()) {
                        baseEntity = (PF_AtomicEntity*)entity;
                        if (baseEntity->getStartpoint().distanceTo(coord) <
                                baseEntity->getEndpoint().distanceTo(coord)) {
                            isStartPoint = true;
                        } else {
                            isStartPoint = false;
                        }
                        setStatus(SetEndAngle);
                        updateMouseButtonHints();
                    } else {
                        // TODO: warning
                    }
                }
                else {
                }
            }
            break;

            // set angle (point that defines the angle)
        case SetEndAngle: {
                PF_CoordinateEvent ce(snapPoint(e));
                coordinateEvent(&ce);
            }
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawArcTangential::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }
    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetBaseEntity:
        break;

    case SetEndAngle:
        point = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawArcTangential::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }
}



PF_StringList WF_DrawArcTangential::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}


void WF_DrawArcTangential::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true);
    }
    updateMouseButtonHints();
}



void WF_DrawArcTangential::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



void WF_DrawArcTangential::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetBaseEntity:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify base entity"),
            tr("Cancel"));
        break;
    case SetEndAngle:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify end angle"), tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawArcTangential::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawArcTangential::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarArcs);
    }
}


// EOF

